package com.lhy.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lhy.domain.Spec;
import com.lhy.goods.service.SpecService;
import com.lhy.goods.mapper.SpecMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lhy
 * @description 针对表【goods_spec(规格表)】的数据库操作Service实现
 * @createDate 2024-07-26 18:50:38
 */
@Service
public class SpecServiceImpl extends ServiceImpl<SpecMapper, Spec>
        implements SpecService {
    @Autowired
    private SpecMapper specMapper;

    @Override
    public List<Spec> selectAll() {
        LambdaQueryWrapper<Spec> specLambdaQueryWrapper =new LambdaQueryWrapper<>();
        specLambdaQueryWrapper.eq(Spec::getIsDelete,0);
        return specMapper.selectList(specLambdaQueryWrapper);
    }

    @Override
    public Integer insert(Spec spec) {
        return specMapper.insert(spec);
    }

    @Override
    public Integer update(Spec spec) {
        return specMapper.updateById(spec);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        String[] split = ids.split(",");
        try {
            for (String id : split) {
                Spec spec =new Spec();
                spec.setIsDelete(1);
                spec.setId(Long.parseLong(id));
                specMapper.updateById(spec);
            }
            return 1;
        } catch (NumberFormatException e) {
            return -1;
        }
    }

    @Override
    public List<Spec> selectList(Spec spec) {
        LambdaQueryWrapper<Spec> lambdaQueryWrapper = this.getLambdaQueryWrapper(spec);
        return specMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public Page<Spec> pageAll(Integer current, Integer size) {
        Page<Spec> page=new Page<>();
        page.setSize(size);
        page.setCurrent(current);
        return this.page(page);
    }

    @Override
    public Page<Spec> pageList(Spec spec, Integer current, Integer size) {
        LambdaQueryWrapper<Spec> lambdaQueryWrapper = this.getLambdaQueryWrapper(spec);
        Page<Spec> page = new Page<>();
        page.setSize(size);
        page.setCurrent(current);
        return this.page(page,lambdaQueryWrapper);
    }

    private LambdaQueryWrapper<Spec> getLambdaQueryWrapper(Spec spec){
        LambdaQueryWrapper<Spec> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (spec.getId()!=null){
            lambdaQueryWrapper.eq(Spec::getId,spec.getId());
        }
        if (spec.getSpecName()!=null){
            lambdaQueryWrapper.eq(Spec::getSpecName,spec.getSpecName());
        }
        if (spec.getSpecValue()!=null){
            lambdaQueryWrapper.eq(Spec::getSpecValue,spec.getSpecValue());
        }
        if (spec.getIsDelete()!=null){
            lambdaQueryWrapper.eq(Spec::getIsDelete,spec.getIsDelete());
        }
        if (spec.getCreateTime()!=null){
            lambdaQueryWrapper.eq(Spec::getCreateTime,spec.getCreateTime());
        }
        if (spec.getUpdateTime()!=null){
            lambdaQueryWrapper.eq(Spec::getUpdateTime,spec.getUpdateTime());
        }

        return lambdaQueryWrapper;
    }
}




